﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using XuanXiang.Infrastructure.Dto.Response;

namespace XuanXiang.Api.Admin.Filters
{
    /// <summary>
    /// 结果过滤器
    /// </summary>
    public class Result_Filter : ActionFilterAttribute
    {
        
        public override Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
        {
            if (context.Result is not Api_Result_Dto) {

                return base.OnResultExecutionAsync(context, next);
            }
            //判断是否是Api_Result_Dto类型
            if (((ObjectResult)context.Result)?.Value is not Api_Result_Dto result)
            {
                //不是直接返回
                return base.OnResultExecutionAsync(context, next);
            }
            if (result.data == null)
            {
                context.Result = new OkObjectResult(new
                {
                    code = result.code,
                    message = result.message,
                });
            }
            return base.OnResultExecutionAsync(context, next);
        }
    }
}
